$currentDir = Get-Location
$dir = $env:windir + "\system32\inetsrv"

Set-Location $dir
$apps = .\appcmd.exe list config /section:system.webServer/fastCgi /xml

$found = $FALSE;
$phpDir = "";

$phpXml = [xml] $apps;
$applicationNodes = $phpXml.SelectNodes("//application[@fullPath[contains(.,'php-cgi.exe')]]");
$count = [int] $applicationNodes.Count;

if ($count -gt 0)
{
    $phpDir = [System.IO.Path]::GetDirectoryName($applicationNodes.Item(0).fullPath);  
}  

if ($phpDir -ne "")
{
$found = $TRUE;
& $env:windir\system32\inetsrv\appcmd set config /section:system.webServer/handlers /+"[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='$phpDir\php-cgi.exe',resourceType='Either']"
}

Set-Location $currentDir

$found;